<?php
if (!function_exists('scanDirectories')) {
	function scanDirectories($rootDir, $recursive = true) {
		//Set filenames invisible if you want
		$invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
		//Run through content of root directory
		$dirContent = scandir($rootDir);
		$allData = array();
		//File counter gets incremented
		$fileCounter = 0;
		foreach($dirContent as $key => $content) {
			//Filter all files not accessible
			$path = $rootDir.'/'.$content;
			if(!in_array($content, $invisibleFileNames)) {
				//If content is a file and it's readable, add it to the array
				if(is_file($path) && is_readable($path)) {
					$tmpPathArray = explode("/",$path);
					//Save the filename
					$allData[$fileCounter]['fileName'] = end($tmpPathArray);
					//Save while path (for better access)
					$allData[$fileCounter]['filePath'] = $path;
					// get file extension
					$filePartsTmp = explode(".", end($tmpPathArray));
					$allData[$fileCounter]['fileExt'] = end($filePartsTmp);
					//Get the file's date of last modification
					$allData[$fileCounter]['fileDate'] = filectime($path);
					//Get the file's size in bytes
					$allData[$fileCounter]['fileSize'] = filesize($path);
					$fileCounter++;
					//If the content is a directory and it's readable, add the path and name
				} elseif ($recursive && is_dir($path) && is_readable($path)) {
					$dirNameArray = explode('/',$path);
					$allData[$path]['dirPath'] = $path;
					$allData[$path]['dirName'] = end($dirNameArray);
					//Open new directory
					$allData[$path]['content'] = scanDirectories($path);
				}	
			}
		}
		return $allData;
	}
}
?>
